#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ObserveTotalProvider : CY.GFive.Core.Providers.DALProvider.IObserveTotalProvider
	{
		#region Sql
		private static readonly string SqlSelectObserveTotal = "SELECT [Id], [UnitCode],[PeerCode],[Num],[NumFinish],[NumAvail],[Des] FROM [ObserveTotal] WHERE  [Id]=@Id";

		private static readonly string SqlInsertObserveTotal = "INSERT INTO [ObserveTotal]([UnitCode],[PeerCode],[Num],[NumFinish],[NumAvail],[Des]) values(@UnitCode,@PeerCode,@Num,@NumFinish,@NumAvail,@Des);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateObserveTotal = "UPDATE [ObserveTotal] SET [UnitCode] = @UnitCode,[PeerCode] = @PeerCode,[Num] = @Num,[NumFinish] = @NumFinish,[NumAvail] = @NumAvail,[Des] = @Des WHERE  [Id]=@Id";

		private static readonly string SqlDeleteObserveTotal = "DELETE [ObserveTotal] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllObserveTotal = "SELECT [Id], [UnitCode],[PeerCode],[Num],[NumFinish],[NumAvail],[Des] FROM [ObserveTotal]";
		#endregion

		#region IObserveTotalProvider Members
		public Core.Business.ObserveTotal Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectObserveTotal);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ObserveTotal observeTotal = new Core.Business.ObserveTotal();

				if (!reader.IsDBNull(0)) observeTotal.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) observeTotal.UnitCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) observeTotal.PeerCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) observeTotal.Num = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) observeTotal.NumFinish = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) observeTotal.NumAvail = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) observeTotal.Des = reader.GetString(6);

				return observeTotal;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ObserveTotal observeTotal)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@UnitCode", SqlDbType.NVarChar, observeTotal.UnitCode);
			sql.AddParameter("@PeerCode", SqlDbType.NVarChar, observeTotal.PeerCode);
			sql.AddParameter("@Num", SqlDbType.Int, observeTotal.Num);
			sql.AddParameter("@NumFinish", SqlDbType.Int, observeTotal.NumFinish);
			sql.AddParameter("@NumAvail", SqlDbType.Int, observeTotal.NumAvail);
			sql.AddParameter("@Des", SqlDbType.NVarChar, observeTotal.Des);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertObserveTotal);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) observeTotal.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ObserveTotal observeTotal)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@UnitCode", SqlDbType.NVarChar, observeTotal.UnitCode);
			sql.AddParameter("@PeerCode", SqlDbType.NVarChar, observeTotal.PeerCode);
			sql.AddParameter("@Num", SqlDbType.Int, observeTotal.Num);
			sql.AddParameter("@NumFinish", SqlDbType.Int, observeTotal.NumFinish);
			sql.AddParameter("@NumAvail", SqlDbType.Int, observeTotal.NumAvail);
			sql.AddParameter("@Des", SqlDbType.NVarChar, observeTotal.Des);
			sql.AddParameter("@Id", SqlDbType.Int, observeTotal.Id);
			sql.ExecuteSql(SqlUpdateObserveTotal);
		}

		public void Delete(Core.Business.ObserveTotal observeTotal)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, observeTotal.Id);
			sql.ExecuteSql(SqlDeleteObserveTotal);
		}

		public IList<Core.Business.ObserveTotal> GetAllObserveTotal()
		{
			IList<Core.Business.ObserveTotal> observeTotallist = new List<Core.Business.ObserveTotal>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllObserveTotal);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ObserveTotal observeTotal = new Core.Business.ObserveTotal();

					if (!reader.IsDBNull(0)) observeTotal.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) observeTotal.UnitCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) observeTotal.PeerCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) observeTotal.Num = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) observeTotal.NumFinish = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) observeTotal.NumAvail = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) observeTotal.Des = reader.GetString(6);

					observeTotal.MarkOld();
					observeTotallist.Add(observeTotal);
				}
				reader.Close();
			}
			return observeTotallist;
		}
		#endregion
	}
}
